﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class PlayerDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static PlayerDAO()
        {
            XmlConfigurator.Configure();
        }

        public static List<PlayerDTO> GetPlayerList()
        {
            try
            {
                List<PlayerDTO> lstPlayer = new List<PlayerDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from player in context.PLAYER_LIST
                             select player);
                foreach (var item in query)
                {
                    item.PLAYER_TYPEReference.Load();
                    item.POSITIONReference.Load();
                    item.TEAMReference.Load();

                    PlayerDTO dtoPlayer = new PlayerDTO();
                    dtoPlayer = Mapper.Map<PLAYER, PlayerDTO>(item);
                    lstPlayer.Add(dtoPlayer);
                }

                return lstPlayer;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }



        public static bool AddNewPlayer(PlayerDTO dtoPlayer)
        {
            try
            {
                PLAYER obj = new PLAYER();
                //Mapping                
                obj.BIRTH_DATE = dtoPlayer.BIRTH_DATE;
                obj.PLAYER_DESC =dtoPlayer .PLAYER_DESC ;
                obj.PLAYER_NAME = dtoPlayer.PLAYER_NAME;
                obj.PLAYER_PICTURE  = dtoPlayer.PLAYER_PICTURE;                
                
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                //Kiểm tra Player Type đã tồn tại chưa, nếu chưa=>phát sinh lỗi                
                obj.PLAYER_TYPE = context.PLAYER_TYPE_LIST.First(pt => pt.PLAYER_TYPE_ID == dtoPlayer.PLAYER_TYPE.PLAYER_TYPE_ID);               
                //Kiểm tra Position đã tồn tại chưa
                obj.POSITION = context.POSITION_LIST.First(p => p.POSITION_ID == dtoPlayer.POSITION.POSITION_ID);
                //Kiểm tra Team đã tồn tại chưa
                obj.TEAM = context.TEAM_LIST.First(t => t.TEAM_ID == dtoPlayer.TEAM.TEAM_ID);                

                context.AddToPLAYER_LIST(obj);
                context.SaveChanges();

                dtoPlayer.PLAYER_ID = obj.PLAYER_ID;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdatePlayer(PlayerDTO dtoPlayer)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                PLAYER Player = context.PLAYER_LIST.First(g => g.PLAYER_ID == dtoPlayer.PLAYER_ID);
                Player.PLAYER_NAME = dtoPlayer.PLAYER_NAME;
                Player.PLAYER_PICTURE = dtoPlayer.PLAYER_PICTURE;
                Player.BIRTH_DATE = dtoPlayer.BIRTH_DATE;
                Player.PLAYER_DESC = dtoPlayer.PLAYER_DESC;
                //Kiểm tra Player type xem có gì mới ko
                Player.PLAYER_TYPEReference.Load();
                if (Player.PLAYER_TYPE.PLAYER_TYPE_ID != dtoPlayer.PLAYER_TYPE.PLAYER_TYPE_ID)
                {
                    Player.PLAYER_TYPE = context.PLAYER_TYPE_LIST.First(pt => pt.PLAYER_TYPE_ID == dtoPlayer.PLAYER_TYPE.PLAYER_TYPE_ID);
                }
                //Kiểm tra Position xem có gì mới ko
                Player.POSITIONReference.Load();
                if (Player.POSITION.POSITION_ID != dtoPlayer.POSITION.POSITION_ID)
                {
                    Player.POSITION = context.POSITION_LIST.First(ps => ps.POSITION_ID == dtoPlayer.POSITION.POSITION_ID);
                }
                //Kiểm tra xem đội bóng có gì mới ko
                Player.TEAMReference.Load();
                if (Player.TEAM.TEAM_ID != dtoPlayer.TEAM.TEAM_ID)
                {
                    Player.TEAM  = context.TEAM_LIST.First(t => t.TEAM_ID  == dtoPlayer.TEAM.TEAM_ID );
                }
                context.SaveChanges(true);
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAPlayer(PlayerDTO dtoPlayer)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                PLAYER  Player = context.PLAYER_LIST .First(p => p.PLAYER_ID  == dtoPlayer .PLAYER_ID);
                context.DeleteObject(Player );
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }


        public static bool DeleteAllPlayer()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from player in context.PLAYER_LIST 
                             select player);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

       
    }
}
